import CryptoJS from 'crypto-js'

function randomString() {
	let result = ''
	const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
	const charactersLength = characters.length
	for (let i = 0; i < 32; i++) {
		result += characters.charAt(Math.floor(Math.random() * charactersLength))
	}
	return result
}
// isheader 指的的是否把加密参数放在header里
export function signfn(url, isHeader = false) {
	const authKey = uni.getStorageSync('organization_info').app_key || '16c23b1bfe3e271c93fde706cbdb2700';

	const identify_key = uni.getStorageSync('organization_info').identify_key || '501e1c2a7ca71c783a8f9132eb4e2d95';

	const authTs = new Date().getTime()

	const authOnce = randomString()

	const toBeEncrypted = `act=${url}&app_key=${authKey}&nonce=${authOnce}&ts=${authTs}&key=${identify_key}`

	const authSign = CryptoJS.MD5(toBeEncrypted).toString()
	return isHeader ? {
		'auth-ts': authTs,
		'auth-nonce': authOnce,
		'auth-sign': authSign,
		'auth-key': authKey,
	} : `${toBeEncrypted}&sign=${authSign}`
}